package qc.module.platform.mapper;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.module.platform.dto.dept.DeptDto;
import qc.module.platform.dto.dept.DeptSimpleDto;
import qc.module.platform.dto.dept.DeptTreeSimpleDto;
import qc.module.platform.dto.dept.DeptTreeTableDetailDto;
import qc.module.platform.entity.QcDepts;

import java.util.List;

/**
 * DeptMapper
 *
 * @author QuCheng Tech
 * @since 2023/5/28
 */
@Mapper
public interface DeptMapper {
    DeptMapper MAPPER = Mappers.getMapper(DeptMapper.class);

    @Mapping(target = "projectcode", source = "pcode")
    @Mapping(target = "odr", source = "order")
    @Mapping(target = "flag", ignore = true)
    QcDepts toEntity(DeptDto dto);

    @Mapping(target = "pcode", source = "projectcode")
    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "order", source = "odr")
    @Mapping(target = "level", ignore = true)
    DeptDto toDto(QcDepts en);


    List<DeptDto> toDtoList(List<QcDepts> list);


    DeptSimpleDto toSimpleDto(DeptDto en);

    List<DeptSimpleDto> toSimpleDtoList(List<DeptDto> list);

    @Mapping(target = "pcode", source = "projectcode")
    @Mapping(target = "flag", source = "flag.index")
    @Mapping(target = "order", source = "odr")
    @Mapping(target = "value", source = "id")
    @Mapping(target = "label", source = "name")
    @Mapping(target = "children", ignore = true)
    @Named("toTreeTableDetailDto")
    DeptTreeTableDetailDto toTreeTableDetailDto(QcDepts en);

    @IterableMapping(qualifiedByName = "toTreeTableDetailDto")
    List<DeptTreeTableDetailDto> toTreeTableDetailDtoList(List<QcDepts> list);

    @Mapping(target = "level", ignore = true)
    @Mapping(target = "children", ignore = true)
    @Named("toTreeSimpleDto")
    DeptTreeSimpleDto toTreeSimpleDto(QcDepts en);

    @IterableMapping(qualifiedByName = "toTreeSimpleDto")
    List<DeptTreeSimpleDto> toTreeSimpleDtoList(List<QcDepts> list);

    /**
     * 新增DTO转换为Entity
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    default QcDepts DeptAddDtoToEntity(DeptDto dto) {
        //DTO转换为Entity
        QcDepts en = DeptMapper.MAPPER.toEntity(dto);

        //排序号需进行判空，非空时进行转换
        if (!StringUtils.isBlank(dto.getOrder())) {
            try {
                int order = Integer.parseInt(dto.getOrder());
                en.setOdr(order);
            } catch (NumberFormatException exception) {
            }
        }
        //判断父栏目ID是否为0，为0设置为null
        if (en.getParentid() == 0x0) {
            en.setParentid(null);
        }

        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (!StringUtils.isBlank(dto.getFlag())) {
            try {
                flag = Integer.parseInt(dto.getFlag());
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        return en;
    }

    /**
     * 修改DTO转换为Entity
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    default QcDepts DeptDtoToEntity(DeptDto dto) {
        //DTO转换为Entity
        QcDepts en = DeptMapper.MAPPER.toEntity(dto);

        //排序号需进行判空，非空时进行转换
        if (!StringUtils.isBlank(dto.getOrder())) {
            try {
                int order = Integer.parseInt(dto.getOrder());
                en.setOdr(order);
            } catch (NumberFormatException exception) {
            }
        }

        //处理标记，非空时进行转换，空值时使用默认
        Integer flag = null;
        if (!StringUtils.isBlank(dto.getFlag())) {
            try {
                flag = Integer.parseInt(dto.getFlag());
            } catch (NumberFormatException exception) {
                //转换错误不处理，使用默认状态
            }
        }
        en.setFlag(getResourceStatusFlagEnum(flag));

        return en;
    }

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return ResourceStatusFlagEnum.NORMAL;
    }
}
